//
//  MaleAndFemaleViewController.m
//  悦读
//
//  Created by dllo on 15/5/23.
//  Copyright (c) 2015年 贾建兴. All rights reserved.
//

#import "MaleAndFemaleViewController.h"
#import <AFNetworking/AFNetworking.h>
#import "SANetWorkingTask.h"
#import "CategoryDetailViewController.h"
//#import <UIImageView+WebCache.h>
@interface MaleAndFemaleViewController ()<UITableViewDelegate, UITableViewDataSource>

// 定义数组, 用来装请求来的tableview上所有行的数据
@property(nonatomic, retain)NSMutableArray *tableArray;
// 定义table属性, 便于使用
@property(nonatomic, retain)UITableView *table;
@end

@implementation MaleAndFemaleViewController

#pragma mark -
- (void)dealloc
{
    [_table release];
    [_tableArray release];
    [_cid release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // 初始化数组
        
        self.tableArray = [NSMutableArray array];
        
    }return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置标题
    if ([self.cid isEqualToString:@"1"]) {
        
        self.navigationItem.title = @"男生";
        
    }
    if([self.cid isEqualToString:@"2"])
    {
        self.navigationItem.title = @"女生";
    }
    
    // 设置导航返回键图片
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"backbefore.png"] style:UIBarButtonItemStylePlain target:self action:@selector(action:)];
    self.navigationItem.leftBarButtonItem = button;
    [button release];
    
    // 创建table
    [self dataRequest];
    [self createTable];
    
}
- (void)action:(id)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 
#pragma mark 建table
- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height   - 64) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    
    [self.view addSubview:self.table];
    UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)] ;
    view.image = [UIImage imageNamed:@"70.jpg"];
    self.table.tableFooterView = view;
    
    
}

#pragma mark -
#pragma mark 申请数据
- (void)dataRequest
{
    NSString *request = @"http://vip.hentr.com/zhuibook/api/zhuibook/GetCategoryList.do";
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"1.3" forKey:@"version"];
    [dic setObject:self.cid forKey:@"cid"];
  [SANetWorkingTask request:request parmaters:dic block:^(NSDictionary *result) {
      self.tableArray = [[result objectForKey:@"response"] objectForKey:@"ReturnData"];
      [self.table reloadData];
     
      
  }];
}
#pragma mark - table的协议实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIn = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIn];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIn];
    }
    
    // 设置点击不变色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // 取出行所对应的数字元素
    NSMutableDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
//    [cell.imageView sd_setImageWithURL:[dic objectForKey:@""] placeholderImage:<#(UIImage *)#>];
    cell.textLabel.text = [dic objectForKey:@"categoryName"];
    return cell;
}

#pragma mark - 
#pragma mark - 点击cell跳转页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    CategoryDetailViewController *detail = [[CategoryDetailViewController alloc]init];
    // 传值
    detail.cid = [dic objectForKey:@"categoryid"];
    detail.choice = 1;
    
    detail.categoryName = [dic objectForKey:@"categoryName"];
    // 跳转页面
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
